package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.service.IActModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.TenderMapper;
import com.ly.heart.domain.TenderEntity;
import com.ly.heart.service.ITenderService;

import javax.annotation.Resource;

/**
 * 投标基础信息，走审批Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class TenderServiceImpl extends ServiceImpl<TenderMapper,TenderEntity> implements ITenderService
{
    @Resource
    private TenderMapper tenderMapper;
    @Resource
    private IActModelService actModelService;

    /**
     * 查询投标基础信息，走审批
     *
     * @param id 投标基础信息，走审批主键
     * @return 投标基础信息，走审批
     */
    @Override
    public TenderEntity selectTenderEntityById(String id)
    {
        return tenderMapper.selectTenderEntityById(id);
    }

    /**
     * 查询投标基础信息，走审批列表
     *
     * @param tenderEntity 投标基础信息，走审批
     * @return 投标基础信息，走审批
     */
    @Override
    public List<TenderEntity> selectTenderEntityList(TenderEntity tenderEntity)
    {
        return tenderMapper.selectTenderEntityList(tenderEntity);
    }

    /**
     * 新增投标基础信息，走审批
     *
     * @param tenderEntity 投标基础信息，走审批
     * @return 结果
     */
    @Override
    public int insertTenderEntity(TenderEntity tenderEntity)
    {
        String tenderId = SnowflakeIdWorker.getInstance().nextStringId();
        tenderEntity.setId(tenderId);
        tenderEntity.setCreateTime(DateUtils.getNowDate());
        StartProcessDto startDto = new StartProcessDto().setBusinessId(tenderId).setBusinessType(tenderEntity.getBusinessType()).setModelId(tenderEntity.getModelId())
        .setBusinessName("投标管理_".concat(tenderId));
        String instanceId = actModelService.startProcess(startDto);
        tenderEntity.setInstanceId(instanceId);
        tenderEntity.setState(ConstUtil.STATE_IN_PROCESS);
        return tenderMapper.insertTenderEntity(tenderEntity);
    }

    /**
     * 修改投标基础信息，走审批
     *
     * @param tenderEntity 投标基础信息，走审批
     * @return 结果
     */
    @Override
    public int updateTenderEntity(TenderEntity tenderEntity)
    {
        tenderEntity.setChangeTime(DateUtils.getNowDate());
        return tenderMapper.updateTenderEntity(tenderEntity);
    }

    /**
     * 批量删除投标基础信息，走审批
     *
     * @param ids 需要删除的投标基础信息，走审批主键
     * @return 结果
     */
    @Override
    public int deleteTenderEntityByIds(String[] ids)
    {
        return tenderMapper.deleteTenderEntityByIds(ids);
    }

    /**
     * 删除投标基础信息，走审批信息
     *
     * @param id 投标基础信息，走审批主键
     * @return 结果
     */
    @Override
    public int deleteTenderEntityById(String id)
    {
        return tenderMapper.deleteTenderEntityById(id);
    }
}
